package com.ruoyi.flowable.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.service.ISysDeployFormService;
import com.ruoyi.flowable.service.ISysBtnService;
import com.ruoyi.system.domain.SysDeployForm;
import com.ruoyi.system.domain.SysBtn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/flowable/btn")
public class SysBtnController extends BaseController {
    @Autowired
    private ISysBtnService SysBtnService;

    @Autowired
    private ISysDeployFormService sysDeployFormService;

    /**
     * 查询流程按钮列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBtn sysBtn) {
        startPage();
        List<SysBtn> list = SysBtnService.selectSysBtnList(sysBtn);
        return getDataTable(list);
    }

    /**
     * 导出流程按钮列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:export')")
    @Log(title = "流程按钮", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysBtn sysBtn) {
        List<SysBtn> list = SysBtnService.selectSysBtnList(sysBtn);
        ExcelUtil<SysBtn> util = new ExcelUtil<SysBtn>(SysBtn.class);
        return util.exportExcel(list, "form");
    }

    /**
     * 获取流程按钮详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:query')")
    @GetMapping(value = "/{formId}")
    public AjaxResult getInfo(@PathVariable("formId") Long formId) {
        return AjaxResult.success(SysBtnService.selectSysBtnById(formId));
    }

    /**
     * 新增流程按钮
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:add')")
    @Log(title = "流程按钮", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBtn sysBtn) {
        return toAjax(SysBtnService.insertSysBtn(sysBtn));
    }

    /**
     * 修改流程按钮
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:edit')")
    @Log(title = "流程按钮", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBtn sysBtn) {
        return toAjax(SysBtnService.updateSysBtn(sysBtn));
    }

    /**
     * 删除流程按钮
     */
    @PreAuthorize("@ss.hasPermi('flowable:btn:remove')")
    @Log(title = "流程按钮", businessType = BusinessType.DELETE)
    @DeleteMapping("/{formIds}")
    public AjaxResult remove(@PathVariable Long[] formIds) {
        return toAjax(SysBtnService.deleteSysBtnByIds(formIds));
    }


    /**
     * 挂载流程按钮
     */
    @Log(title = "流程按钮", businessType = BusinessType.INSERT)
    @PostMapping("/addDeployForm")
    public AjaxResult addDeployForm(@RequestBody SysDeployForm sysDeployForm) {
        return toAjax(sysDeployFormService.insertSysDeployForm(sysDeployForm));
    }
}
